package cn.hxth.cloud.provider.updater;

import cn.hxth.cloud.api.common.util.DateProvider;
import cn.hxth.cloud.api.domain.Steels;
import cn.hxth.cloud.api.mapper.SteelsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractSteelsUpdaterFactory<T> implements SteelsUpdaterFactory<T> {
    private Logger logger = LoggerFactory.getLogger(AbstractSteelsUpdaterFactory.class) ;
    protected DateProvider dateProvider ;
    protected SteelsMapper steelsMapper ;

    public AbstractSteelsUpdaterFactory(DateProvider dateProvider, SteelsMapper steelsMapper) {
        this.dateProvider = dateProvider;
        this.steelsMapper = steelsMapper ;
    }

    @Override
    public SteelsUpdater create(T parameter) {
        try{
            List<Long> ids = this.getSteelsIds(parameter);
            List<Steels> steeles = this.createSettles(ids);
            return  this.createSteelsUpdater(steeles,parameter) ;
        }catch (Exception e){
        logger.error("error",e);
        return  null ;
        }


    }
    protected abstract List<Long> getSteelsIds(T parameter) throws Exception ;
    protected abstract SteelsUpdater createSteelsUpdater(List<Steels> steels, T parameter)throws Exception ;
    private List<Steels> createSettles( List<Long> ids)throws Exception{
        List<Steels> steelses = new ArrayList<>(ids.size()) ;
        for(long id:ids){
            Steels steels = this.steelsMapper.selectByPrimaryKey(id);
            steelses.add(steels) ;
        }
        return  steelses ;
    }
}
